package com.proj;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;

import java.util.jar.Manifest;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.SpringVersion;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.MethodInvoker;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ClassUtils;
import org.springframework.core.convert.*;
import org.springframework.core.convert.support.ConversionServiceFactory; 
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.io.*;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		/*
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		Fruit f = (Fruit)context.getBean("fruit");
		f.play();
		Fruit f2 = (Fruit)context.getBean("fruit2");
		f2.play();
		*/
		
		/*
		ResourceLoader rl = new DefaultResourceLoader();
		Resource r =  rl.getResource("classpath:applicationContext.xml"); 
		
		BeanFactory bf = new XmlBeanFactory(r);
		Fruit f = (Fruit)bf.getBean("fruit");
		f.play();
		Fruit f2 = (Fruit)bf.getBean("fruit2");
		f2.play();
		*/
		
	}
	
	private static void play() {
		Core core = new Core();
		 //core.core();
		 System.out.println(ClassUtils.classNamesToString(Core.class,Main.class));
		 System.out.println(ClassUtils.classPackageAsResourcePath(Core.class));
		 System.out.println(ClassUtils.convertClassNameToResourcePath("com.proj.Core"));
		 System.out.println(ClassUtils.getClassFileName(Core.class));
		 System.out.println(ClassUtils.hasMethod(Core.class, "core",String.class));
		 System.out.println(Arrays.toString(ClassUtils.getAllInterfaces(core)));
		 System.out.println(SpringVersion.class.getPackage().getImplementationVersion());
		 
		 //GenericConversionService cs = ConversionServiceFactory.createDefaultConversionService();
		 //System.out.println(cs.canConvert(java.util.Date.class, Class.class));
		   
		  
		 Object obj = null;
		 if (obj instanceof FactoryBean) {
			 
		 }
	}
	
	private static interface FactoryBean {
		
	}
	
	private static void methodInvoke() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		//调用方法的不同方式
		MethodInvoker mi = new MethodInvoker();
		//mi.setStaticMethod("com.proj.Core.core");
		//mi.setTargetClass(Core.class);    
		mi.setTargetMethod("core");
		mi.setTargetObject(new Core());
		mi.setArguments(new Object[]{"argument..."});
		mi.prepare();
		mi.invoke();
	}
	
	private static void reflectInvokeMethod() {
		Core core = new Core();
		Method coreMethod = ReflectionUtils.findMethod(Core.class, "core",String.class);
		System.out.println(coreMethod);
		ReflectionUtils.invokeMethod(coreMethod, core,"String.");
	}
	
	private static void reateJarFile() throws Exception {
		JarFile jarFile = new JarFile("C:/Documents and Settings/lijiepeng/桌面/lib/zk_libs2/org.springframework.core-3.0.3.RELEASE.jar");
		Enumeration<? extends ZipEntry> jarEntrys = jarFile.entries();
		for (;jarEntrys.hasMoreElements();) {
			//System.out.println(jarEntrys.nextElement().getName());
		}
		Manifest manifest = jarFile.getManifest();
		Map<String,Attributes> map = manifest.getEntries();
		 
		for (Map.Entry<String, Attributes> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "," + entry.getValue());
		}
	}
	
	private static void readZipFile() throws Exception {
		ZipFile zipFile = new ZipFile("C:/Documents and Settings/lijiepeng/桌面/新建文件夹 (2)/TraFaxBoardServer.zip");
		System.out.println(zipFile.size());
		 
		Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
		
		for (;zipEntrys.hasMoreElements();) {
			System.out.println(zipEntrys.nextElement().getName());
		}
	}
	
	 private static void createZipFile() throws Exception{
		 File file = new File("C:/Documents and Settings/lijiepeng/桌面/新建文件夹 (2)/aa");
		  System.out.println(Arrays.toString(file.list()));
		  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File("C:/Documents and Settings/lijiepeng/桌面/新建文件夹 (2)/TraFaxBoardServer2.zip")));
		  for (String s : file.list()) {
			  ZipEntry ze = new ZipEntry(s);
			  out.putNextEntry(ze); 
			  FileInputStream in = new FileInputStream(file.getAbsolutePath() + "/" + s);
			  
			  int byteCount = 0;
				byte[] buffer = new byte[4096];
				int bytesRead = -1;
				while ((bytesRead = in.read(buffer)) != -1) {
					out.write(buffer, 0, bytesRead);
					byteCount += bytesRead;
				}
				out.flush();
			    in.close();
		  }
		  out.close();
	 }
}
